package framework.utils;

import framework.controller.*;
import framework.event.CustomEvent;
import framework.utils.dto.ActionCommand;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.concurrent.RunnableFuture;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @version 0.5
 * @author Michel
 * Class for handling incoming GameServer data
 */
public class GameServerListener implements Runnable {
	
	public GameServerListener() {};
	
	/**
	 * Run while socket isConnected
	 * @method run
	 */
	@Override
	public void run() {
		while(ServerController.SOCKET.isConnected()) {
			this.readCommand();
		}
	};

	/**
	 * Read data from GameServer
	 * @method readCommand
	 */
	public void readCommand() {
		try {
			String line = ServerController.INPUT_READER.readLine();
			if(line != null) {
				//Print line
				System.out.println(line);
				//SERVER REQUEST RESPONSE 
//				if(line.indexOf("OK") != -1 || line.indexOf("SVR") != -1) {
					ServerController._STACK.add(new ActionCommand(ServerController.CURRENT_LISTENER, line));
//				}
			}
		} catch(Exception e) {
			e.printStackTrace();
			System.out.println(this.getClass().getName() + " -> readCommand() -> " + e.getMessage());
		};
	};

}
